package com.mef.smsplat.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mef.smsplat.dao.CompanyInfoDao;
import com.mef.smsplat.dao.GroupInfoDao;
import com.mef.smsplat.dao.PersonInfoDao;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.EntityDao;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.CompanyInfoDomain;
import com.mef.smsplat.domain.GroupInfoDomain;
import com.mef.smsplat.domain.PersonInfoDomain;
import com.mef.smsplat.service.base.BaseService;

@Service
@Transactional
public class GroupInfoService extends BaseService<GroupInfoDomain, String> {
	
	private GroupInfoDao groupInfoDao;
	private PersonInfoDao personInfoDao;
	private CompanyInfoDao companyInfoDao;
	
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setGroupInfoDao(GroupInfoDao groupInfoDao) {
		this.groupInfoDao =groupInfoDao;
	}
	public void setPersonInfoDao(PersonInfoDao personInfoDao) {
		this.personInfoDao =personInfoDao;
	}
	public void setCompanyInfoDao(CompanyInfoDao companyInfoDao) {
		this.companyInfoDao =companyInfoDao;
	}
	@Override
	public EntityDao getEntityDao() {
		return this.groupInfoDao;
	}
	
	/**
	 * 根据组名分页查询信息
	 * @param groupName
	 * @param dgm
	 * @return
	 */
	@Transactional(readOnly=true)
	public Page query(String groupName,DataGridModel dgm){
		Page<GroupInfoDomain> page = groupInfoDao.query(groupName, dgm);
		return page;
	}
	
	/**
	 * 查询是否唯一
	 * @param groupName
	 * @param id
	 * @return
	 */
	@Transactional(readOnly=true)
	public boolean isUnique(String groupName, String id){
		boolean isUnique = groupInfoDao.queryByNiAndId(groupName, id);
		return isUnique;
	}
	
	@Transactional
	public void addGroup(GroupInfoDomain group,String[] personIds,String[] companyIds,String id){
		if(personIds!=null)
			groupInfoDao.addPersonMember(personIds, id);
		if(companyIds!=null)
		groupInfoDao.addCompanyMember(companyIds, id);
	}
	
	/**
	 * 删除组关联里面的记录
	 * @param ids
	 */
	@Transactional
	public void deleteRelateGroup(String[] ids){
		groupInfoDao.deleteMember(ids);
	}
	
	@Transactional(readOnly=true)
	public List<GroupInfoDomain> queryAll(){
		return groupInfoDao.queryAll();
	}
	
	/**
	 * 根据组id查询组内成员
	 * @param groupId
	 * @param dgm
	 * @return
	 */
	@Transactional(readOnly=true)
	public Page queryPerson(String groupId,DataGridModel dgm){
		Page<PersonInfoDomain> page = personInfoDao.queryMember(groupId, dgm);
		return page;
	}
	
	/**
	 * 根据组id查询组内成员
	 * @param groupId
	 * @param dgm
	 * @return
	 */
	@Transactional(readOnly=true)
	public Page queryCompany(String groupId,DataGridModel dgm){
		Page<CompanyInfoDomain> page = companyInfoDao.queryMember(groupId, dgm);
		return page;
	}
}
